//
//  FocusViewController
//  HiveApp
//
//  Created by liquan on 12-9-19.
//  Copyright (c) 2012年 HiveView. All rights reserved.
//

#import "FocusView.h"
#import "EasyBuyRequest.h"
#import "FocusData.h"
#import "FocusScrollView.h"
#import "EasyBuyService.h"

@interface FocusView(Private)<UIScrollViewDelegate>

@end

@implementation FocusView

@synthesize focusScrollView;
@synthesize focusTitleField;
@synthesize focusDotView;
@synthesize focusIntercourseBtn;
@synthesize focusDataArray;

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if(self){
        
        FocusScrollView *_focusScrollView = [[FocusScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _focusScrollView.showsVerticalScrollIndicator = NO;
        _focusScrollView.showsHorizontalScrollIndicator = NO;
        _focusScrollView.pagingEnabled = YES;
        _focusScrollView.delegate = self;
        self.focusScrollView = _focusScrollView;
        [_focusScrollView release];
        [self addSubview:focusScrollView];
        
        [self initFocusViewData];
        
        DotView *_focusDotView = [[DotView alloc] initWithFrame:CGRectMake(0, frame.size.height-15, frame.size.width,15)];
        self.focusDotView = _focusDotView;
        [self addSubview:focusDotView];
        [_focusDotView release];
        
        
    }
    return  self;
}

/**
    @author Arashmen 20120927
 
 */

-(void)initFocusViewData{
    
    EasyBuyService *service =  [[EasyBuyService alloc] init];
    EasyBuyRequest<RequestProtocol> *request = [[EasyBuyRequest alloc] initFocusRequest];
    [service executeFocusRequest:request :RECEIVER_HOME_FOCUS];

    [[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(onReceivedFocusData:)
												 name:RECEIVER_HOME_FOCUS
											   object:nil];

}

-(void)onReceivedFocusData:(NSNotification *)notification{
    NSArray *_focusArray = notification.object;
    [self.focusScrollView makeFocuses:_focusArray];
    [self.focusDotView makeDots:_focusArray];
    self.focusDataArray = _focusArray;
    [_focusArray release];
    [self scrollViewDidEndDecelerating:self.focusScrollView];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGPoint point = scrollView.contentOffset;
    CGFloat x = point.x;
    int currentPage = (int)(x/scrollView.bounds.size.width);
    [self.focusDotView dotFocused:currentPage];
    
}


@end
